<?php
/**
 * (logic) 运营报表-公司产品利润率
 */
namespace App\Logic\Operation;

use App\Models\Basics\AmazonCountry;
use App\Models\Basics\ExchangeRate;
use App\Models\Basics\SysICountry;
use App\Models\Basics\Transport;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductPacking;
use App\Models\Product\ProductSku;
use Illuminate\Support\Facades\DB;
class CalculateProfitSku
{
    /**
     * 计算公司产品利润率
     */
    public static function handle()
    {
        $rate = [];
        $area = [];
        $transportFee = [];
        $unitPrice = [];
        $weight = [];
        $vat = [];
        ProfitSku::query()
            ->orderBy('id')
            ->chunk(800, function ($items) use (&$rate,&$area,&$transportFee,&$unitPrice,&$weight,&$vat){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        if(!array_key_exists($item->country_id,$rate)){
                            $currencyCode = AmazonCountry::getCurrencyCodeByCountryId($item->country_id);
                            $rate[$item->country_id] = ExchangeRate::getRateOddsToRMB($currencyCode,time());
                        }
                        $item->rate_odds = $rate[$item->country_id];
                        if(!array_key_exists($item->country_id,$area)){
                            $area[$item->country_id] = SysICountry::getAreaIdByCountryId($item->country_id);
                        }
                        $areaId = $area[$item->country_id];
                        $key = $areaId .'_'. $item->transport;
                        if(!array_key_exists($key,$transportFee)){
                            $transportFee[$key] = Transport::getShipmentFee($areaId,$item->transport);
                        }
                        if(!array_key_exists($item->psku_id,$unitPrice)){
                            $ps = ProductSku::getModelById($item->psku_id);
                            $unitPrice[$item->psku_id] = ProductSku::getPuPriceNoTax($ps);
                        }
                        $item->tpl_rmb = $unitPrice[$item->psku_id];
                        $tpl = round($item->tpl_rmb / $item->rate_odds,3);
                        if(!array_key_exists($item->psku_id,$weight)){
                            $weight[$item->psku_id] = ProductPacking::getShipWeight($item->psku_id);
                        }
                        if(!array_key_exists($item->country_id,$vat)){
                            $vat[$item->country_id] = AmazonCountry::getVatByCountryId($item->country_id);
                        }
                        $item->vat = $vat[$item->country_id];
                        $item->weight = $weight[$item->psku_id];
                        $item->head_cost = round($item->weight * $transportFee[$key] / $item->rate_odds,3);
                        $item->cost = ProfitSku::getCost($tpl,$item->sale_price,$item->head_cost,$item->fba_fee,$item->commission,$item->return,$item->vat);
                        $item->profit = round($item->sale_price - $item->cost,3);
                        $item->profit_rate = $item->sale_price == 0 ? 0 : round($item->profit / $item->sale_price,3);
                        $fbaFeeFlag = $item->country_id == 20 ? false : true;
                        $item->status = ProfitSku::getStatus($item->status,$item->sale_price,$item->fba_fee,$item->commission,$fbaFeeFlag,$item->created_at);
                        $item->save();
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
        });
    }
}
